<!-- 企业类型（富民） -->
<template>
    <el-cascader
            :options="orgList"
            :props="propsOrg"
            @active-item-change="OFhandleItemChange"
            @change="Ochange"
            :value="initValue"
            :show-all-levels="false"
    ></el-cascader>
</template>

<script lang="ts">
    import { Vue, Component } from 'vue-property-decorator';
    @Component({})
    export default class OrgType extends Vue {
        orgList = []            // options：可选数据源
        propsOrg = {               // props：是一个对象，相关配置如下
            value: 'orgTypeCode',
            label: 'orgTypeName',
            children: 'orgList'
        }
        initValue = []         // value：选中项绑定值

        // active-item-change：当父级选项发生变化是触发，各父级选项组成的数组（所以在调用的时候，默认参数要传数组）
        async OFhandleItemChange (ids) {
            if(ids.length === 0){
                this.orgList = await this.getOrg('', false);
            }else if (ids.length === 1){
                for(let i = 0; i < this.orgList.length; i++){
                    if (this.orgList[i].orgTypeCode == ids[0]){
                        if(this.orgList[i].optional == 1){
                            this.orgList[i].orgList = await this.getOrg(ids[0], false);
                        }else if(this.orgList[i].optional == 0){
                            this.orgList[i].orgList = await this.getOrg(ids[0], true);
                        }
                    }
                }
            }else if(ids.length === 2){
                for (let i = 0; i < this.orgList.length; i++) {
                    if (this.orgList[i].orgTypeCode === ids[0]) {
                        for (let j = 0; j < this.orgList[i].orgList.length; j++) {
                            if (this.orgList[i].orgList[j].orgTypeCode === ids[1]) {
                                this.orgList[i].orgList[j].orgList = await this.getOrg(ids[1], true);
                            }
                        }
                    }
                }
            }
        }

        // 获取客户类型
        async getOrg (orgTypeCode: string, isEnd: boolean) {
            let params = {
                orgTypeCode: orgTypeCode
            }
            let response = await this.$http.get('fbank/constants/orgType',{params},{ loading: false })
            if(!isEnd){
                response.data.forEach(v => v.orgList = []);
            }
            return response.data;
        }
        mounted () {
            this.OFhandleItemChange([]);
        }
        // change：当绑定事件触发时，回调是当前值
        Ochange (areas) {
            this.$emit('input', areas);
        }
        async init (initValue) {
            this.initValue = initValue
            setTimeout(async () => {
                if (this.initValue.length == 2){
                    for (let i = 0; i < this.orgList.length; i ++){
                        if (this.orgList[i].orgTypeCode == initValue[0]){
                            this.orgList[i].orgList = await this.getOrg(initValue[0], true);
                            // for (let j = 0; j < this.orgList[i].orgList; j ++){
                            //     this.orgList[i].orgList[j].orgList = await this.getOrg(initValue[1], true);
                            // }
                        }
                    }
                }else if (this.initValue.length == 3){
                    for (let i = 0; i < this.orgList.length; i ++){
                        if (this.orgList[i].orgTypeCode == initValue[0]){
                            this.orgList[i].orgList = await this.getOrg(initValue[0], false);
                            for (let j = 0; j < this.orgList[i].orgList; j ++){
                                this.orgList[i].orgList[j].orgList = await this.getOrg(initValue[1], true);
                            }
                        }
                    }
                }

            }, 400)
            this.$emit('input', initValue);
        }
    }
</script>

<style scoped>

</style>